$(function() {
	/**
	 * 键盘点击事件，回车键，执行搜索
	 */
	$("#search_key").bind('keypress', function(event) {
		if (event.keyCode == "13") {
			var searchKey = $("#search_key").val().trim();
			if(searchKey) {
				$("#product_search_form").submit();
			} else {
				window.location.href = rootPath + '/pc/product/search?categoryId=1';
			}
		}
	});
	
	// 异步加载滚动广告图片
	var url = rootPath + "/pc/listIndexRollingAD";
	$.ajax({
		type: 'POST',
		url: url,
		async: true,
		data: {},
		dataType: 'json',
		success: function(json) {
			var slideBox = $("#slideBox");
			var digits =$("#slide_no");
			var pics = $("#slide_content");
			if (json.length > 0) {
				digits.find("li").remove();
				pics.find("li").remove();
				for(var i = 0 ; i < json.length; i++){
					digits.append("<li>"+(i+1)+"</li>");
					if(json[i].linkUrl && json[i].linkUrl.indexOf('http') != -1) {
						pics.append("<a target='_blank' href='"+json[i].linkUrl+"'><li style='background: url("+imgRootURL+"/"+json[i].imagePath+") no-repeat top center'></li></a>");
					} else {
						pics.append("<li style='background: url("+imgRootURL+"/"+json[i].imagePath+") no-repeat top center'></li>");
					}
					
				}
				slideBox.slide({mainCell:".bd ul",autoPlay:true});
			}
		}
	})
	
	// 异步加载用户的订单统计数量
	url = rootPath + "/pc/listCustomerOrderStatistics";
	$.ajax({
		type: 'POST',
		url: url,
		async: true,
		data: {},
		dataType: 'json',
		success: function(json) {
			if(json){
				$("#to_pay_num").text(numberInteger(json['TO_PAY_NUM']));
				$("#to_send_num").text(numberInteger(json['TO_SEND_NUM']));
				$("#to_receive_num").text(numberInteger(json['TO_RECEIVE_NUM']));
				$("#overdued_num").text(numberInteger(json['OVERDUED_NUM']));
				$("#cur_week_num").text("￥"+numberInteger(json['CUR_WEEK_NUM']));
				$("#cur_month_num").text("￥"+numberInteger(json['CUR_MONTH_NUM']));
			}
		}
	})
	
	
	//  异步查询热门搜索关键字
	url = rootPath + "/pc/listHotSearchKey";
	$.ajax({
		type: 'POST',
		url: url,
		async: true,
		data: {},
		dataType: 'json',
		success: function(json) {
			if(json){
				for(var i = 0; i < json.length; i ++){
					var searchKey = $("<a href='javascript:void(0)' >"+json[i].keyIntro+"</a>");
					searchKey.click(function(){
						$("#search_key").val($(this).text());
						$("#product_search_form").submit();
					});
					$("#hot_search_key_name").after(searchKey);
				}
			}
		}
	});
	
	var ids = [];
	$("b[id^='dyna_price']").each(function(){
		ids.push($(this).attr("id").substring(11));
	});
	url = rootPath + "/pc/listFloorProductPrice";
	$.ajax({
		type: 'POST',
		url: url,
		async: true,
		data: {productIds:ids.join(',')},
		dataType: 'json',
		success: function(json) {
			if(json){
				for(var i = 0; i < json.length; i ++){
					if(json[i].basePrice){
						$("#dyna_price_"+json[i].id).text("￥"+numberRand2(parseFloat(json[i].productPrice)));
					}
				}
			}
		}
	});
	
	/**
	 * 获取购物车数量
	 */
	$.ajax({
		type: 'POST',
		url: rootPath + "/pc/shoppingcart/getShoppingcarCount",
		async: true,
		dataType: 'json',
		success: function(json) {
			if(json.code == '1') {
				$("#shopCount").html(json.data);
			}
		}
	});
});

/**
 * 我的订单
 */
function toMyOrderStatus(orderStatus) {
	$.ajax({
		type: 'POST',
		url: rootPath + "/home/isLogin",
		async: true,
		dataType: 'json',
		success: function(json) {
			if(json.code == '1') {
				window.location.href =  rootPath + '/pc/order/list?orderStatus='+orderStatus;
			} else {
				// layer.msg('请先登录！',{time:1000});
				window.location.href=rootPath+'/home/loginPage';
			}
		}
	});
}

/**
 * 一级分类下多个小分类筛选
 */
function doProductSearch(categoryId) {
	var brandLabelArr = $(".brand_ul .brand_ul_" + categoryId + " label.checked");
	var brandIdStr = '';
	if(brandLabelArr && brandLabelArr.length > 0) {
		for(var i = 0; i < brandLabelArr.length; i ++) {
			if(i == (brandLabelArr.length - 1)) {
				brandIdStr += brandLabelArr[i].title;
			} else {
				brandIdStr += brandLabelArr[i].title + ',';
			}
		}
	}
	var categoryArr = $(".xz_part .temp_ul_" + categoryId + " label.checked");
	var categoryIdStr = '';
	for (var i = 0; i < categoryArr.length; i++) {
		// categoryIdStr += categoryArr[i].value + ",";
		categoryIdStr += categoryArr[i].title + ",";
//		if (categoryArr[i].checked) {
//			categoryIdStr += categoryArr[i].value + ",";
//		}
	}
	if (categoryIdStr || brandIdStr) {
		$(".xz_part .checked").removeClass('checked');
		$("input[type=button][id^='btnBrandCategorySearch_']").css('background-color', 'gray');
		window.open(rootPath + "/pc/product/search?categoryId=" + categoryIdStr + "&brandIds=" + brandIdStr);
	}
}



